package top.usking.tools.log.config;

import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.usking.tools.log.interceptor.OutputLogMethodInterceptor;

/**
 * AOP log auto configuration.
 * @author LiuShijin
 * @date 2022-01-28
 */
@Configuration
@EnableConfigurationProperties({AopLogProperties.class, CustomerLogService.class})
@ConditionalOnProperty(prefix = "top.usking.log", name = "enabled", havingValue = "true")
public class AopLogAutoConfiguration {
    private final AopLogProperties properties;

    public AopLogAutoConfiguration(AopLogProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnProperty(prefix = "top.usking.log", name = "enabled", havingValue = "true")
    public AspectJExpressionPointcutAdvisor aspectJExpressionPointcutAdvisor() {
        AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
        advisor.setExpression(properties.getLogPointcutExpression());
        advisor.setAdvice(aopAdvice());
        return advisor;
    }

    @Bean
    public OutputLogMethodInterceptor aopAdvice() {
        return new OutputLogMethodInterceptor(customerLogService());
    }

    @RefreshScope
    @Bean
    public CustomerLogService customerLogService() {
        return new CustomerLogService();
    }
}

